﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering_Rules;
using Catering.View_Operaction;
using System.Data;
using Microsoft.Reporting.WebForms;
using Catering_Model;
using System.Text;

namespace Catering.DisplayBoards
{
    public partial class DietStructure_fldb2 : System.Web.UI.Page
    {
        #region 属性
        List<CheckBox> listCheckBox = null;//存储页面所有CheckBox
        private static string areaId = "";//作业区ID
        private static string classId = "";//班组ID
        private static string[] years;//年份
        private static int[] RawId = { 3, 4, 6, 7, 8, 10, 11 };//食品大类ID
        //FromRecords frRules = new FromRecords();
        View_Monitor vm = new View_Monitor();
        //T_RawMaterials rawMaterialsRule = new T_RawMaterials();
        //OperationArea OAreaRules = new OperationArea();
        //ClassInfo ClassRules = new ClassInfo();
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadForm();
            }
        }
        //定时刷新页面
        protected void Btn_Ref_Click(object sender, EventArgs e)
        {
            this.LoadForm();
        }
        protected void LoadForm()
        {
            areaId = "4";
            classId = "-10";
            DateTime year_current = DateTime.Now; //当前年月
            string TimeType = "Month";
            if (year_current == DateTime.MinValue) return;
            switch (TimeType)
            {
                case "Month":
                    GetXiaohaoCharByMonth(areaId, classId, year_current, RawId);
                    break;
            }
        }
        public Dictionary<string, decimal> GetReportModel(string groupid, string classid)
        {
            Dictionary<string, decimal> result = new Dictionary<string, decimal>();
            Dictionary<int, string> temp = GetClassList(groupid, classid);
            foreach (string value in temp.Values)
            {
                if (!result.ContainsKey(value)) result.Add(value, 0);
            }
            return result;
        }
        public Dictionary<int, string> GetClassList(string groupid, string classid)
        {
            Dictionary<int, string> result = new Dictionary<int, string>();

            Dictionary<int, string> temp = UCDDLToolWithYearOrMonthOrDay1.GetAllDDL_Area();
            List<Catering_Model.ClassInfo> listClass = new Catering_Rules.ClassInfo().GetAllList(0);
            if (temp.Count > 0)
            {
                foreach (int id in temp.Keys)
                {
                    foreach (Catering_Model.ClassInfo item in listClass.Where(c => c.AreaID == id))
                    {
                        result.Add(item.id, item.CName);
                    }
                }
            }
            
            return result;
        }
        string[] GetRawName = { "肉类", "鱼虾类", "蔬菜类", "水果类", "谷类薯类及杂豆" };
        private void GetXiaohaoCharByMonth(string areaId, string classid, DateTime DTIME, int[] RawId)
        {
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            DataTable dt = vm.GetXiaohao_ContrastByMonth(pid, areaId, classid, DTIME, RawId);
            string Title = Bind_Data_to_Controls.Bind_ReportTitle(areaId, classId, DTIME, "消耗分类对比");
            Dictionary<string, Dictionary<string, decimal>> zhonglei = new Dictionary<string, Dictionary<string, decimal>>();

            foreach (string rawName in GetRawName)
            {
                Dictionary<string, decimal> list = GetReportModel(areaId, classId);//班组列表(x轴列表)
                if (!zhonglei.ContainsKey(rawName)) zhonglei.Add(rawName, list);
            }

            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int tempclassid = Convert.ToInt32(dt.Rows[i]["classid"]);
                    string className = dt.Rows[i]["cname"].ToString();
                    decimal SumNumber = Convert.ToInt32(dt.Rows[i]["SumNumber"]);
                    string typename = dt.Rows[i]["typename"].ToString();
                    if (zhonglei.ContainsKey(typename))
                    {
                        zhonglei[typename][className] = decimal.Round(SumNumber / 1000);
                    }
                }
                dt.Dispose();
            }

            string StrX = "";
            string str = "";
            if (zhonglei != null)
            {
                int n = 0;
                StringBuilder Sresult = new StringBuilder("");
                foreach (string raw in zhonglei.Keys)
                {
                    Sresult.Append("{");
                    Sresult.Append("name:'" + raw + "',");
                    Sresult.Append("data: [");
                    string StrData = "";
                    foreach (string key in zhonglei[raw].Keys)
                    {
                        if (n == 0) StrX += "'" + key + "',";
                        StrData += zhonglei[raw][key] + ",";
                    }
                    StrData = StrData.Length > 0 ? StrData.Substring(0, StrData.Length - 1) : "";
                    Sresult.Append(StrData + "]");
                    if (n == 0) StrX = StrX.Length > 0 ? StrX.Substring(0, StrX.Length - 1) : "";
                    Sresult.Append("},");
                    str = Sresult.ToString();
                    n++;
                }

            }
            StrX = "[" + StrX + "]";
            str = str.Length > 0 ? str.Substring(0, str.Length - 1) : "";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "GetDataGroup('" + Title + "'," + StrX + "," + str + ");", true);
        }
    }
}